//
//  CfgViewController.swift
//  MTBLETools
//
//  Created by 方强 on 16/11/27.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit

class CfgViewController: UIViewController {

    static func getActivity() -> CfgViewController{
        let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main);
        let vc = storyBoard.instantiateViewController(withIdentifier: "CfgViewController") as! CfgViewController;
        
        return vc;
    }
    private var mGlobalData:StaticDatas!;
    override func viewDidLoad() {
        super.viewDidLoad()
        mGlobalData = StaticDatas.getInstance();
        // Do any additional setup after loading the view.
        
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(keyboardHide));
        tapGestureRecognizer.cancelsTouchesInView = false;
        self.view.addGestureRecognizer(tapGestureRecognizer);
    }
    
    func keyboardHide(tap:UITapGestureRecognizer){
        self.view.endEditing(true);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private var baseAlert: UIAlertView!;
    @IBOutlet weak var user_spinertmp: UIView!
    private var user_spiner: ISGDropDownList?
    private let USERTYPES = ["智能","MTBeacon","馒头透传模块"];
    @IBOutlet weak var conectpwd_edit: UITextField!
    private func initView(){
        
        if(user_spiner != nil){
            return;
        }
        
        user_spiner = ISGDropDownList(frame: CGRect(x: user_spinertmp.frame.minX, y: user_spinertmp.frame.minY, width: user_spinertmp.frame.width, height: user_spinertmp.frame.height));
        user_spiner!.listItems = USERTYPES;
        
        self.view.addSubview(user_spiner!);
        
        user_spiner?.selectedIndex = mGlobalData.mixDatas.usertype;
        conectpwd_edit.text = mGlobalData.mixDatas.beaconPwd;
    }

    @IBAction func navAction(_ sender: UIBarButtonItem) {
        if(sender.tag == 1){  // 返回
            self.dismiss(animated: false, completion: { 
                
            });
            return;
        }
        if(sender.tag == 2){  // 确定
            
            if((conectpwd_edit.text == nil) || (conectpwd_edit.text!.lengthOfBytes(using: String.Encoding.utf8) != 6)){
                showFailAlert(erromsg: "密码必须为6位");
                return;
            }
            
            mGlobalData.mixDatas.beaconPwd = conectpwd_edit.text!;
            mGlobalData.mixDatas.usertype = user_spiner!.selectedIndex;
            
            MixDatas.updata(database: mGlobalData.db, device: mGlobalData.mixDatas);
            
            self.dismiss(animated: false, completion: { 
                
            });
            
            return;
        }
    }
    
    // 显示失败对话框
    private func showFailAlert(erromsg: String){
        baseAlert = UIAlertView(title: "Fail", message: erromsg, delegate: nil, cancelButtonTitle: "OK");
        baseAlert.show();
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    @IBAction func TextFinish(_ sender: UITextField) {
        sender.resignFirstResponder();
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        self.view.openKeyboardOffsetView();
        
        initView();
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated);
        self.view.closeKeyboardOffsetView();
    }
}
